package ui;

import bean.Component;
import bean.ComponentStateBean;
import utils.AndroidUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class ColumnDialog extends JDialog {

    public static final int ITEM_HEIGHT = 25;
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPanel header;
    private JScrollPane scroll;
    private JPanel list;
    private JPanel footer;
    private JCheckBox cbAll;

    public ColumnDialog(ArrayList<Component> strings) {
        AndroidUtils.centerDialog(this, 800, 600);
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        scroll.setPreferredSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        scroll.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));
        list.setLayout(new BoxLayout(list, BoxLayout.Y_AXIS));
        // header
        setTitle("组件管理");
        createHeader();


        //重置一下组件安装的状态
        ArrayList<Component> components = resetComponents(strings);

        HashMap<String, Integer> count = new HashMap<>();

        for (Component component : components) {
            createItem(component, count);
        }
        createFooter();

        buttonOK.addActionListener(e -> performGenerate());
        buttonCancel.addActionListener(e -> dispose());
    }

    /**
     * 遍历组件，重置一下每个组件的挂载状态
     */
    private ArrayList<Component> resetComponents(ArrayList<Component> strings) {
        HashMap<String, Boolean> states = AndroidUtils.handlerComponents(strings);
        Set<String> keySet = states.keySet();
        for (Component string : strings) {
            if (keySet.contains(string.getGroup())) {
                boolean result = (string.isEnable() && states.get(string.getGroup()));
                string.setEnable(result);
            }
        }
        return strings;
    }


    private void createHeader() {
        header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
        header.add(Box.createHorizontalStrut(4));

        JLabel name = new JLabel("组件分组");
        name.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        header.add(name);
        header.add(Box.createHorizontalGlue());

        JLabel attribute = new JLabel("组件属性");
        attribute.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        header.add(attribute);

        header.add(Box.createHorizontalGlue());

//        JLabel type = new JLabel("组件类型");
//        type.setPreferredSize(new Dimension(140, ITEM_HEIGHT));
//        header.add(type);
//
//        header.add(Box.createHorizontalGlue());

        JLabel isEnable = new JLabel("是否启用");
        header.add(isEnable);
        header.add(Box.createHorizontalStrut(10));
    }

    private void createItem(Component component, HashMap<String, Integer> count) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, ITEM_HEIGHT));

        panel.add(Box.createHorizontalStrut(2));

        //组件分组
        JLabel name = new JLabel(component.getGroup());
        name.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        panel.add(name);

        panel.add(Box.createHorizontalGlue());

        // 组件属性
        JLabel attribute = new JLabel(component.getAttribute());
        attribute.setPreferredSize(new Dimension(210, ITEM_HEIGHT));
        panel.add(attribute);

        panel.add(Box.createHorizontalGlue());

//        //组件类型
//        JLabel type = new JLabel(component.getType());
//        type.setPreferredSize(new Dimension(140, ITEM_HEIGHT));
//        panel.add(type);
//
//        panel.add(Box.createHorizontalGlue());

        JCheckBox checkBox = new JCheckBox();
        checkBox.setPreferredSize(new Dimension(40, ITEM_HEIGHT));
        boolean enable = component.isEnable();


        if (enable) {
            checkBox.setSelected(true);
            if (!component.getType().equalsIgnoreCase("功能组件")) {
                checkBox.setEnabled(false);
            } else {
                ComponentStateBean bean = new ComponentStateBean();
                bean.setName(component.getName());
                bean.setCheckBox(checkBox);
                bean.setEnable(true);
                bean.setGroup(component.getGroup());
                functions.add(bean);
                checkBox.addActionListener(listener);
            }
        } else {
            if (!component.getType().equalsIgnoreCase("功能组件")) {
                ComponentStateBean core = new ComponentStateBean();
                core.setName(component.getName());
                core.setCheckBox(checkBox);
                core.setEnable(false);
                core.setGroup(component.getGroup());
                coreComponents.add(core);
            } else {
                ComponentStateBean bean = new ComponentStateBean();
                bean.setName(component.getName());
                bean.setCheckBox(checkBox);
                bean.setEnable(false);
                bean.setGroup(component.getGroup());
                functions.add(bean);
            }
            checkBox.addActionListener(listener);
        }
        panel.add(checkBox);
        panel.add(Box.createHorizontalStrut(10));

        count.put(component.getGroup(), (count.get(component.getGroup()) != null ? (Integer) count.get(component.getGroup()) : 0) + 1);

        //如果分组的次数大于1次了 则不用重复添加了
        if (count.get(component.getGroup()) > 1) {
            return;
        }

        list.add(panel);
    }

    private final MyActionListener listener = new MyActionListener();
    private final ArrayList<ComponentStateBean> functions = new ArrayList<>();


    /**
     * 如果是核心组件或者核心UI，在设置勾选状态的时候，就会设置成checkbox不可用的状态，强制不让用户再次改变。
     * <p>
     * 特别是会议UI组件和voipUI组件是依赖组件，如果勾选了会议ui组件，需要自动勾选viopUI组件，相反则不能。
     */
    private class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            String groupName = null;
            boolean result = false;
            for (ComponentStateBean coreComponent : coreComponents) {
                if (coreComponent.getCheckBox() == source) {
                    coreComponent.setEnable(true);
                    coreComponent.getCheckBox().setEnabled(false);
                }
            }
            for (ComponentStateBean function : functions) {
                if (function.getCheckBox() == source) {
                    JCheckBox checkBox = (JCheckBox) source;
                    result = checkBox.isSelected();
                    //如果会议组件，并且设置的是勾选状态，则自动勾选voipUI
                    function.setEnable(result);
                    //并且设置同组的组件为相同的状态
                    if (result && function.getName().equalsIgnoreCase("SCP_MeetingUI")) {
                        for (ComponentStateBean functionComponent : functions) {
                            if (functionComponent.getName().equalsIgnoreCase("SCP_VoipUI")) {
                                JCheckBox voip = functionComponent.getCheckBox();
                                voip.setSelected(true);
                                functionComponent.setEnable(true);

                            }
                        }
                    }
                    groupName = function.getGroup();
                    break;
                }
            }

            //同步更新一下同组组件的状态
            for (ComponentStateBean function : functions) {
                if (function.getGroup().equalsIgnoreCase(groupName)) {
                    function.setEnable(result);
                }
            }
        }
    }

    private final ArrayList<ComponentStateBean> coreComponents = new ArrayList<>();

    private void createFooter() {
        footer.setLayout(new BoxLayout(footer, BoxLayout.X_AXIS));
        footer.add(Box.createHorizontalStrut(10));

        cbAll = new JCheckBox("全选");
        cbAll.setSelected(false);
        cbAll.addActionListener(e -> {
            for (ComponentStateBean function : functions) {
                function.setEnable(cbAll.isSelected());
                function.getCheckBox().setSelected(cbAll.isSelected());
            }
            for (ComponentStateBean coreComponent : coreComponents) {
                coreComponent.setEnable(true);
                coreComponent.getCheckBox().setSelected(true);
                coreComponent.getCheckBox().setEnabled(false);
            }
        });
        footer.add(cbAll);
    }

    //回调到主类中
    private void performGenerate() {
        dispose();
        if (onGenerateListener != null) {
            onGenerateListener.onGenerate(functions, coreComponents);
        }
    }

    private OnGenerateListener onGenerateListener;

    public void setOnGenerateListener(OnGenerateListener onGenerateListener) {
        this.onGenerateListener = onGenerateListener;
    }

    public interface OnGenerateListener {
        void onGenerate(ArrayList<ComponentStateBean> functions, ArrayList<ComponentStateBean> cores);
    }
}
